package com.hitqz.robot.dispatch.netty.service.handle;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author kehanjiang
 * @date 2023/11/03
 */
@Component
public class RuleHandleFactory {
    private Map<String, IRuleHandle> handleMap;
    private List<IRuleHandle> handleList;
    private List<String> supportTypes;

    public RuleHandleFactory(List<IRuleHandle> ruleHandleList) {
        handleList = ruleHandleList.stream()
                .filter(x -> x.getType() != null)
                .collect(Collectors.toList());

        handleMap = new HashMap<>(handleList.size());
        handleList.stream().forEach(x -> handleMap.put(x.getType(), x)
        );
        supportTypes = handleList.stream().map(x -> x.getType()).collect(Collectors.toList());
    }


    public List<IRuleHandle> getHandleList() {
        return handleList;
    }

    public List<String> getSupportTypes() {
        return supportTypes;
    }

    public IRuleHandle getRuleHandle(String type) throws RuntimeException {
        if (handleMap.containsKey(type)) {
            return handleMap.get(type);
        } else {
            throw new RuntimeException(String.format("不支持的类型：%s", type));
        }
    }

}
